package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.EsService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @ClassName EsController
 * @Author RC
 * @date 2021.01.01 21:47
 */
@Controller
@RequestMapping("es")
public class EsController {
    @Reference(timeout = 10000,check = false)
    private ItemService itemService;
    @Reference(timeout = 10000,check = false)
    private EsService esService;
    private static final Logger logger = LoggerFactory.getLogger(EsController.class);

    @RequestMapping("addEsSource")
    @ResponseBody
    public void addEsSource() throws IOException {
       List<TbItem> itemList = itemService.getAll();
       esService.addSource(itemList);

    }

    @RequestMapping("getInfoBySearch")
    @ResponseBody
    public DataResult getInfoBySearch(String search, @RequestParam(defaultValue = "1")Integer pageNumber, @RequestParam(defaultValue = "10")Integer pageSize) {
        try {
            logger.info("进入es模糊查询方法");
            PageBean<TbItem> itemList = esService.getInfoBySearch(search,pageNumber,pageSize);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(itemList);

        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
